/*
 * FileServer.cpp
 *
 *  Created on: 30.03.2011
 *      Author: niea
 */

#include "FileServer.hpp"

#include <cstdlib>
#include <string>

#include <signal.h>
#include <sys/types.h>

namespace efs
{

FileServer::FileServer(std::string const& root_path, int file_port)
	: FileServerI(root_path, file_port)
{
}

FileServer::~FileServer()
{
}

void * FileServer::threadProcStarter(void *base_class_pointer)
{
	static_cast<FileServer*> (base_class_pointer)->threadProc();
	return 0;
}

void FileServer::threadProc()
{
	system(std::string("cd " + m_root_path + "&& python -m SimpleHTTPServer " + intToStr(m_file_port) + "").c_str());
	kill(0, 2);
}


bool FileServer::start()
{
	if (pthread_create (&m_thread, NULL, FileServer::threadProcStarter, (void*)this))
	{
		pantheios::log_ERROR("[FileServer]: thread starting - error creating thread");
		return false;
	}
	pantheios::log_INFORMATIONAL("[FileServer]: thread start");
	return true;
}

void FileServer::stop()
{
	pantheios::log_INFORMATIONAL("[FileServer]: stop request");
	pthread_join(m_thread, NULL);
}

}
